package cn.jcet.gumall.product.service.impl;

import cn.jcet.gumall.product.dao.BrandDao;
import cn.jcet.gumall.product.dao.CategoryDao;
import cn.jcet.gumall.product.entity.BrandEntity;
import cn.jcet.gumall.product.entity.CategoryEntity;
import cn.jcet.gumall.product.service.BrandService;
import cn.jcet.gumall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.jcet.common.utils.PageUtils;
import cn.jcet.common.utils.Query;

import cn.jcet.gumall.product.dao.CategoryBrandRelationDao;
import cn.jcet.gumall.product.entity.CategoryBrandRelationEntity;
import cn.jcet.gumall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 鲜磊
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDetil(CategoryBrandRelationEntity categoryBrandRelation) {
        /**
         * 1、单表联查，先获取品牌的分类的id
         * 2、根据id查询，获取名称
         */
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = this.brandDao.selectById(brandId);
        categoryBrandRelation.setBrandName(brandEntity.getName());

        CategoryEntity categoryEntity = this.categoryDao.selectById(catelogId);
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Transactional
    @Override
    public void updateBrand(Long brandId, String name) {
//        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        CategoryBrandRelationEntity brandRelationEntity = new CategoryBrandRelationEntity();
        brandRelationEntity.setBrandId(brandId);
        brandRelationEntity.setBrandName(name);
        // 同步更新
        this.update(brandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Transactional
    @Override
    public void updateCategory(Long catId, String name) {
//        this.baseMapper.updateDetail(catId,name);

        CategoryBrandRelationEntity brandRelationEntity = new CategoryBrandRelationEntity();
        brandRelationEntity.setCatelogId(catId);
        brandRelationEntity.setCatelogName(name);

        this.update(brandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelogId",name));
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        /**
         * 1、根据catid查询
         * 2、使用属性copy的方式
         * 3、将这个list返回
         */
        List<CategoryBrandRelationEntity> catelogId = this.baseMapper.
                selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandEntity> brandEntities = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity entity = this.brandService.getById(brandId);
            return entity;
        }).collect(Collectors.toList());


        return brandEntities;
    }
}